﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;

namespace Medianamik.UI.Web
{
    public partial class Login2 : System.Web.UI.Page
    {
        private const string SUPER_ADMIN_USERNAME = "sa";
        private TextBox _txtUsername = null;

        protected void Page_Init(object sender, EventArgs e)
        {
            BuildPage();
        }

        private void BuildPage()
        {
            var html = new System.Web.UI.HtmlControls.HtmlGenericControl("html");
            var head = new System.Web.UI.HtmlControls.HtmlHead("head");
            var title = new System.Web.UI.HtmlControls.HtmlGenericControl("title") { InnerText = "Login2" };
            var form = new System.Web.UI.HtmlControls.HtmlForm();
            var body = new System.Web.UI.HtmlControls.HtmlGenericControl("body");
            
            var div1 = new System.Web.UI.HtmlControls.HtmlGenericControl("div");
            var p1 = new System.Web.UI.HtmlControls.HtmlGenericControl("p");
            
            var btnSa = new Button()
            {
                Text ="Go",
                ID = "btnSA"
            };
            btnSa.Click += btnSA_Click;

            var p2 = new System.Web.UI.HtmlControls.HtmlGenericControl("p");

            _txtUsername = new TextBox()
            {
                CssClass = "TxtBox",
                ID = "txtUsername"
            };

            var btnUsername = new Button ()
            {
                ID="btnUsername",
                Text="Go"
            };
            btnUsername.Click+= btnUsername_Click ;

            var validator = new CustomValidator()
            {
                ID="cvUsername",
                ValidateEmptyText = true,
                ControlToValidate = "txtUsername",
                ErrorMessage = "L'utilisateur n'existe pas",
                Display = ValidatorDisplay.Dynamic 
            };

            validator.ServerValidate+= cvUsername_ServerValidate;


            p1.Controls.Add(new LiteralControl("Se connecter en tant que SA "));
            p1.Controls.Add(btnSa);

            p2.Controls.Add(new LiteralControl("Autre utilisateur "));
            p2.Controls.Add(_txtUsername);
            p2.Controls.Add(new LiteralControl(" "));
            p2.Controls.Add(btnUsername);
            p2.Controls.Add(new LiteralControl(" "));
            p2.Controls.Add(validator);

            div1.Controls.Add(p1);
            div1.Controls.Add(p2);
            
            form.Controls.Add(div1);

            body.Controls.Add(form);
            html.Controls.Add(head);
            html.Controls.Add(body);
            Page.Controls.Add(html);

        }

        protected void Page_Load(object sender, EventArgs e)
        {
            
           
        }

        protected void btnSA_Click(object sender, EventArgs e)
        {
            FormsAuthentication.SetAuthCookie(SUPER_ADMIN_USERNAME, false);
            Response.Redirect("Index.aspx"); 
        }

        protected void btnUsername_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                FormsAuthentication.SetAuthCookie(_txtUsername.Text, false);
                Response.Redirect("Index.aspx");
            }
        }

       protected void cvUsername_ServerValidate(object source, ServerValidateEventArgs args)
        {
            if (Membership.GetUser(args.Value) == null)
            {
                args.IsValid = false;
            }
            else
            {
                args.IsValid = true;
            }
           

        }

    }
}
